// pages/biz/payment/payment.js
//引入请求接口文件
import Serv from './paymentServ';
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
import Util from '../../../../utils/util2.js'
let goodsList = [
  { actEndTime: '2018-10-06 17:14:00' }
]
Page({

  /**
   * 页面的初始数据 seckillOrderId=1054205222815612928&actionType=2
   */
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
      'http://down.laifudao.com/tupian/201712414735.jpg'
    ],
    // action数值决定显示哪个页面: 1:秒杀=>提交报名费 ; 2:秒杀=>支付定金 ; 3:竞拍=>支付报名费 ; 4:竞拍=>支付定金
    action:2,
    // payState数值决定支付状态: 1:支付中(默认) ; 2:支付失败 ; 3:支付超时
    payState:1,
    showExplain:'',
    showDeposit:'',
    showEnsure:'',
    // 倒计时
    countDown: {},
    endTime:0,
    securityMoneyInfo:{},
    disabled: false,
    productDeType: [4, 5, 6, 7],
    productHouseType: [1, 2, 3]
  },
  //报名费请求
  async securityMoney(parmas,cbk){
    let data = await Serv.securityMoney(parmas)
    console.log(data)
    cbk&&cbk(data)
  },
  // 点击支付
  confirmForm(e){
    if (e.detail.value.checked !='agree'){
      wx.showToast({
        title:'请阅读支付协议',
        icon: 'none'
      })
    }else{
      if (this.data.action == 1 || this.data.action == 3){
        //发起支付报名费请求
        let { seckillProductId, securityMoneyInfo, action } = this.data
        let parmas = {
          seckillProductId,
          accountId: wx.getStorageSync('userId'),//todo 用户id
          name: securityMoneyInfo.accountName
        }
        let payParmas = `id=${seckillProductId}&price=${securityMoneyInfo.securityMoney}&name=${securityMoneyInfo.accountName}&action=${action}`
        // 页面传id和price和action （1:秒杀=>提交报名费 ; 2:秒杀=>支付定金 ; 3:竞拍=>支付报名费 ; 4:竞拍=>支付定金）   和 name用户名
        wx.navigateTo({
          url: `../payForBank/payForBank?${payParmas}`,
        })
      } else if (this.data.action == 2 || this.data.action == 4){
        //发起支付定金请求
        let { seckillOrderId, seckillProductId, amount } = this.data.earnestMoneyInfo
        let payParmas = `id=${seckillProductId}&orderId=${seckillOrderId}&price=${amount}&action=${this.data.action}`
        // 页面传id和price和action （1:秒杀=>提交报名费 ; 2:秒杀=>支付定金 ; 3:竞拍=>支付报名费 ; 4:竞拍=>支付定金）   和 name用户名
        wx.navigateTo({
          url: `../payForBank/payForBank?${payParmas}`,
        })
      }
    } 
  },

  // 支付失败
  payFail(){
    this.setData({ payState: 2 })
    wx.setNavigationBarTitle({ title: '支付失败' })
  },
  // 支付超时
  payOvertime(){
    this.setData({ payState: 3 })
    wx.setNavigationBarTitle({ title: '支付超时' })
  },
  //订单请求 获取报名费订单信息
  async querySecurityMoneyInfo(params){
    console.log(params)
    let { data, success,msg} = await Serv.querySecurityMoneyInfo(params)
    console.log(data)
    if (success){
      data.securityMoney = String(data.securityMoney)
      this.setData({
        securityMoneyInfo: data
      })
    }else{
      wx.showToast({
        title: msg,
        icon:'none',
        complete:function(){
          setTimeout(()=>{
            wx.navigateBack({
              delta: 2
            })
          },500)
        }
      })
      this.setData({
        disable:true
      })
    }
  },
  //订单请求 获取 秒杀/竞拍 定金 订单信息
  async getOrderDetail(params){
    let { data, success,msg } = await Serv.queryOrderDetail(params)
    if (success) {
      data.amount = data.amount.toFixed(2)
      let endTime = Util.dateFromString(data.payTimeOut).getTime() 
      this.setData({
        earnestMoneyInfo: data,
        endTime: endTime
      })
      this.countDown()
    } else {
      wx.showToast({
        title: msg,
        icon: 'none'
      })
      this.setData({
        disable: true
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      action: options.actionType,
      seckillProductId: options.seckillProductId || 0 ,
      seckillOrderId : options.seckillOrderId || 0
    })
  },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    // 对结束时间进行处理渲染到页面
    let endTime = this.data.endTime
    let obj = null;
    // 如果活动未结束，对时间进行处理
    if (endTime - newTime > 0) {
      let time = (endTime - newTime) / 1000;
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24));
      let hou = parseInt(time % (60 * 60 * 24) / 3600);
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
      obj = {
        day: this.timeFormat(day),
        hou: this.timeFormat(hou),
        min: this.timeFormat(min),
        sec: this.timeFormat(sec)
      }
      // 渲染，然后每隔一秒执行一次倒计时函数
      this.setData({ countDown: { ...obj } })
      this.setData({ timeOut: setTimeout(this.countDown, 1000) })
    } else {//活动已结束，全部设置为'00'
      obj = {
        day: '00',
        hou: '00',
        min: '00',
        sec: '00'
      }
      //切换至支付超时
      this.payOvertime()
      this.setData({ countDown: { ...obj } })
      clearTimeout(this.data.timeOut)
    }
    console.log(obj)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 根据标识来判断当前页面显示
    if (this.data.action == 1) {
      this.setData({ showExplain: true, showDeposit: false, showEnsure: false})
      wx.setNavigationBarTitle({ title: '提交报名费' })
      //查询报名费订单信息
      let params = {
        seckillProductId: this.data.seckillProductId,
        accountId: wx.getStorageSync('userId')//todo 用户id
      }
      this.querySecurityMoneyInfo(params)

    } else if (this.data.action == 2) { 
      this.setData({ showExplain: false, showDeposit: true, showEnsure: false})
      wx.setNavigationBarTitle({ title: '支付定金' })
      //查询定金订单信息
      let seckillOrderId = this.data.seckillOrderId
      this.getOrderDetail(seckillOrderId)

    } else if (this.data.action == 3) {
      this.setData({ showExplain: false, showDeposit: false, showEnsure: true})
      wx.setNavigationBarTitle({ title: '支付报名费' })
      //查询订单信息
      let params = {
        seckillProductId: this.data.seckillProductId,
        accountId:wx.getStorageSync('userId')//todo 用户id
      }
      this.querySecurityMoneyInfo(params)

    } else if (this.data.action == 4) {
      this.setData({ showExplain: false, showDeposit: true, showEnsure: false})
      wx.setNavigationBarTitle({ title: '支付定金' })
      let seckillOrderId = this.data.seckillOrderId
      this.getOrderDetail(seckillOrderId)
    }
    this.getqueryDetail(this.data.seckillProductId)
  },
  async getqueryDetail(parmas){
    //支付页面在这里set进去localStroage
    let { data, success, msg } = await Serv.getqueryDetail(parmas)
    if(success){
      wx.setStorage({
        key: 'currentDetail',
        data: data,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(this.data.timeOut)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timeOut)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})